<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<%
	String path = request.getContextPath();
%>
<title>所有签约查询(服务器)</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/btn_class.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/style.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/ext-patch.css" />

<script type="text/javascript"
	src="<%=path%>/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=path%>/extjs/ext-all.js"></script>

<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/lovcombo-1.0/lovcombo.css" />
<script type="text/javascript" src="<%=path%>/extjs/lovcombo-1.0/lovcombo.js"></script>

	<script type="text/javascript" src="<%=path%>/js/datepicker/WdatePicker.js" defer="defer"></script>
	<script type="text/javascript" src="<%=path%>/public/public.js" defer="defer"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
	<div id="west-div" style="width: 100%; height: 100%;"></div>
	<div id="center-div"></div>
</body>
<script type="text/javascript">
	Ext.BLANK_IMAGE_URL = "<%=path%>/extjs/resources/images/default/s.gif";

	Ext.onReady(function() {
				Ext.QuickTips.init();
				
				// shorthand
				var Tree_op = Ext.tree;
				var loader_op = new Tree_op.TreeLoader({
					dataUrl : 'office!buildOfficeTree.do'
				});
				loader_op.on("beforeload", function(treeLoader, node) {
					//loader_op.baseParams.fcode = Ext.get("r_id").dom.value;//角色id
					//loader.baseParams.fcode  = node.attributes.fcode;
				}, this);

				loader_op.processResponse = function(response, node, callback) {
					var json1 = response.responseText;
					
					try {
						var json = eval("(" + json1 + ")");
						node.beginUpdate();
						//从json中取得json数组
						var o = json["key"];
						for ( var i = 0, len = o.length; i < len; i++) {
							var n = this.createNode(o[i]);
							if (n) {
								node.appendChild(n);
							}
						}
						node.endUpdate();
						if (typeof callback == "function") {
							callback(this, node);
						}
					} catch (e) {
						this.handleFailure(response);
					}
				};

				var tree_op = new Tree_op.TreePanel({
					region : 'center',
					id : 'west-panel',
					el : 'west-div',
					width : 280,
					//	title : '所属区县',
					useArrows : true,
					autoScroll : true,
					animate : true,
					enableDD : true,
					containerScroll : true,
					rootVisible : false, //树根不可见
					loader : loader_op
				});
				
				//单击事件
				tree_op.on('click', function(node) {
						contentpanel.setTitle("当前机构--><font color=blue>" + node.text + "</font>");
						Ext.get('foffice_flag').dom.value = node.id;
						// ds_user.removeAll();
						ds_user.on('beforeload', function(store) {
							this.baseParams = {
									'limit' : 50,
									'fparent' : node.id
								};
						});
						ds_user.load({
							params : {
								start : 0
							}
						});
					
				}, tree_op);
				
				/*
				//checkBox勾选事件
				tree_op.on('checkchange', function(node, checked) {
					node.expand();
					node.attributes.checked = checked;
					node.eachChild(function(child) {
						child.ui.toggleCheck(checked);
						child.attributes.checked = checked;
						child.fireEvent('checkchange', child, checked);
					});
					
					var checkedNodes = tree_op.getChecked();
					  var codeStr = "";
					
					  for(i=0;i<checkedNodes.length;i++) {
					      var checkeNode = checkedNodes[i];
					      codeStr = "'" + checkeNode.attributes.id + "'," + codeStr;
					  }
					 
					Ext.get("foffice_id").dom.value = codeStr+"'-100'";
					
				}, tree_op);
                */
                
				// set the root node
				var root_op = new Tree_op.AsyncTreeNode({
					text : '所有栏目',
					draggable : false,
					id : '-1',
					fid : '-1',
					ftype : '-1'
				});
				tree_op.setRootNode(root_op);

				
	var Jthtwb = Ext.data.Record.create([ {
		name : 'fid',
		mapping : 'fid',
		type : 'string'
	}, {
		name : 'fname',
		mapping : 'fname',
		type : 'string'
	}, {
		name : 'fcode',
		mapping : 'fcode',
		type : 'string'
	}, {
		name : 'fparent',
		mapping : 'fparent',
		type : 'string'
	}, {
		name : 'ftel',
		mapping : 'ftel',
		type : 'string'
	}, {
		name : 'fadmin',
		mapping : 'fadmin',
		type : 'string'
	}, {
		name : 'fadtel',
		mapping : 'fadtel',
		type : 'string'
	}, {
		name : 'fadd_form',
		mapping : 'fadd_form',
		type : 'string'
	} ]);

	//用户列表
	var ds_user = new Ext.data.Store({
		reader : new Ext.data.JsonReader({
			totalProperty : 'totalProperty',
			root : 'root'
		}, Jthtwb),
		idProperty : 'threadid',
		remoteSort : true,
		url : 'office!queryOfficeList.do'
	});
	

	//定义勾选框，不需要可不必定义
	var sm_user = new Ext.grid.CheckboxSelectionModel({
		singleSelect : true
	});

	//定义列
	var colM_user = new Ext.grid.ColumnModel([ sm_user, new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
	    {
			header : '机构名称',
			dataIndex : 'fname',
			width : 320,
			renderer : function(value) {
					if (value == "null") {
						return "";
					} else {
						return value;
					}
				}
		} ]);

	//生成表格
	var grid_user = new Ext.grid.GridPanel({
		region : 'center',
		height : 350,
		autoWidth : true,
		stripeRows : true,
		
		cm : colM_user,
		ds : ds_user,
		sm : sm_user,
		listeners : {
			'rowclick' : function(grid, row) { //双击打开
				var record = grid.getSelectionModel().getSelected();
				if (record) {
					Ext.get("fparent_id").dom.value = record.data.fparent;
					Ext.get("fname_form").dom.value = record.data.fname;
					Ext.get("fcode_form").dom.value = record.data.fcode;
					if(record.data.fadmin == null) {
						Ext.get("fadmin_form").dom.value = record.data.fadmin;
					}
					if(record.data.fadtel == null) {
						Ext.get("fadtel_form").dom.value = record.data.fadtel;
					}
					if(record.data.ftel == null) {
						Ext.get("ftel_form").dom.value = record.data.ftel;
					}
					if(record.data.fadd == null || record.data.fadd == "undefined") {
						Ext.get("fadd_form").dom.value = record.data.fadd;
					}
					
				}
			}
		},
		enableHdMenu : false
	});
	
	var btn_add = new Ext.Button({
		text : '新增',
		iconCls : 'sy-add',
		handler : function() {
			user_detail_form();
		}
	});
	
	var btn_delete = new Ext.Button({
		text : '删除',
		iconCls : 'sy-delete',
		handler : function() {
			var record = grid_user.getSelectionModel().getSelected();
			if (record) {
				Ext.Msg.confirm('确认删除', '删除操作将会导致某些数据无法正常显示，您确定删除吗？', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url : 'office!deleteOffice.do',
							params:{
			    	        	fcode : record.data.fcode
				    	    },
							success : function() {
								ds_user.removeAll();
								ds_user.reload();
								Ext.MessageBox.alert('提示', '删除成功！');
							},
							failure : function() {
								Ext.Msg.show({
									title : '错误提示',
									msg : '删除记录失败！',
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.ERROR
								});
							}
						});
					}
				});
			} else {
				Ext.MessageBox.alert('提示', '请选择一行记录进行操作！');
				return;
			}
		}
	});
	
	var btn_refresh = new Ext.Button({
		text : '刷新',
		iconCls : 'sy-refresh',
		handler : function() {
			ds_user.removeAll();
			ds_user.reload();
		}
	});
	
	
					var office_form_send = new Ext.FormPanel(
							{
								layout : "form",  
								url : 'office!saveOffice.do',
								method : 'POST',
								labelWidth : 75,
						        waitMsgTarget: true,  
						        bodyStyle : 'padding-left:15px;',
								resizeTabs : true,
								frame : true,
								items : [{
									xtype : 'hidden',
									fieldLabel : '上级机构id',
									name : 'fparent',
									id : 'fparent_id'
									//value : Ext.get("foffice_flag").dom.value
								},{
									xtype : 'textfield',
									fieldLabel : '机构名称',
									id : 'fname_form',
									name : 'fname',
									readOnly : false,
									allowBlank : false,
									emptyText : '请填写机构名称',
									blankText : '请填写机构名称',
									width : 230
								},{
									xtype : 'textfield',
									fieldLabel : '联系人',
									id : 'fadmin_form',
									name : 'fadmin',
									readOnly : false,
									allowBlank : true,
									width : 130
								},{
									xtype : 'numberfield',
									fieldLabel : '联系人手机',
									id : 'fadtel_form',
									name : 'fadtel',
									readOnly : false,
									allowBlank : true,
									width : 130
								},{
									xtype : 'textfield',
									fieldLabel : '联系人电话',
									id : 'ftel_form',
									name : 'ftel',
									readOnly : false,
									allowBlank : true,
									width : 130
								},{
									xtype : 'textarea',
									fieldLabel : '详细地址',
									id : 'fadd_form',
									name : 'fadd',
									readOnly : false,
									allowBlank : true,
									height : 90, 
									width : 230
								},{
									xtype : 'hidden',
									fieldLabel : 'fcode',
									id : 'fcode_form',
									name : 'fcode'
								}
						]});
						
					var btn_save_form = new Ext.Button({
						text : '保存',
						iconCls : 'sy-save',
						handler : function(btn) {
							Ext.get("fparent_id").dom.value = Ext.get("foffice_flag").dom.value;
							var frm = office_form_send.getForm();
							if (frm.isValid()) {
								frm.submit({
									url : 'office!saveOffice.do',
									method : 'POST',
									waitTitle : '请稍候',
									waitMsg : '正在提交表单数据,请稍候...',
									success : function(form, action) {
											Ext.MessageBox.alert('提示', '保存成功！');
											ds_user.reload();
									},
									failure : function(form, action) {
										Ext.Msg.show({
											title : '错误提示',
											msg : action.result.message,
											buttons : Ext.Msg.OK,
											fn : function() {
												unfield.focus(true);
												btn.enable();
											},
											icon : Ext.Msg.ERROR
										});
									},// failure
									icon : Ext.Msg.ERROR
								});
							}
							;// isValid
						}
					});
					
					var btn_reset_form = new Ext.Button({
						text : '清空',
						iconCls : 'sy-reset',
						handler : function(btn) {
							office_form_send.reset();
						}

					});

	
     var panel = new Ext.Panel({
			    	region: "center",
					layout:'fit',
					border : true,
					margins:'0 5 0 0',
					tbar : [ btn_save_form ],
					items : [ office_form_send ]
				});
				
				
				var btn_refresh_office = new Ext.Button({
					text : '刷新',
					iconCls : 'sy-refresh',
					handler : function(btn) {
						tree_op.root.reload();
					}
				});
				
				var panelmenu = new Ext.Panel({
					region: "west",
					split:true,
		            minSize: 105,
		            maxSize: 400,
		            collapsible: true,
		            collapsed: false, //展开或关闭
		            margins:'0 0 0 0',
		            layout:'fit',
		            layoutConfig:{
		                animate:true
		            },
		            tbar : [ '->', btn_refresh_office ],
					items :[tree_op],
					width:280
			   });
			   
			   var rightpanel = new Ext.Panel({
					region: "west",
					split:true,
		            minSize: 105,
		            maxSize: 400,
		            collapsible: true,
		            collapsed: false, //展开或关闭
		            margins:'0 0 0 0',
		            layout:'fit',
		            layoutConfig:{
		                animate:true
		            },
		            tbar : [ btn_delete, '-', btn_refresh , {
						xtype : 'hidden',
						id : 'foffice_flag',
						name : 'foffice_flag'
					}],
					items : [ grid_user ],
					width:280
			   });
			   
			   var contentpanel = new Ext.Panel({
					region: "center",
					layout:'border',
		            layoutConfig:{
		                animate:true
		            },
		            title : '当前机构',
		            tbar : [ '->' ],
					items :[rightpanel, panel],
					width:380
			   });
				
				
				/************************************* 所属区县树 结束 ********************************************************/
				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [ panelmenu, contentpanel ]
				});
				
				
			});
</script>


</html>